//
//  LGDeviceSendSearchController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/9.
//

#import "LGDeviceSendSearchController.h"
#import "LGDeviceSendDetailController.h"
#import "LGDeviceSendViewCell.h"
@interface LGDeviceSendSearchController ()<UITableViewDelegate,UITableViewDataSource,LGDeviceSendViewDelegate>
/**   */
@property (nonatomic , strong)UIView *topView;
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UIView *leftView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;
@end

@implementation LGDeviceSendSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isfirst = NO;
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.searchTF];
    [self.topView addSubview:self.cancelBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self.emptyView stopBufferAction];
    [self requestData];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.searchTF becomeFirstResponder];
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/pagedList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20",@"keyword":self.searchTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}



//取消
-(void)cancelSearchAction{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)textFieldEditingChanged:(UITextField *)textField{
    [self requestData];
    
}

//取消订单
-(void)cancelSendDeviceAction:(NSDictionary *)dataDic index:(NSInteger)index{
    __block NSInteger cancelIndex = index;
    __block NSString *receptid = LGNSString(dataDic[@"receipt_id"]);
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"取消订单" content:@"确定取消该订单吗？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/cancel" parameters:@{@"receipt_id":receptid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                if(self.deviceCancelSuccess){
                    self.deviceCancelSuccess();
                }
                [weakSelf.dataArray removeObjectAtIndex:cancelIndex];
                [weakSelf.tableView reloadData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"操作失败，请重试" offset:0];
        }];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGDeviceSendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
}

#pragma mark
#pragma mark ====> 创建控件
-(UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight)];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(12), statusBarHeight+6, Screen_W-viewPix(72), 32)];
        _searchTF.backgroundColor = [UIColor colorWithString:@"#F3F3F3"];
        _searchTF.cornerRidus = 5;
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.returnKeyType = UIReturnKeySearch;
        _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTF.placeholder = @"输入患者姓名或登记号搜索";
        _searchTF.font = LGFont(15);
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = self.leftView;
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}

-(UIView *)leftView{
    if(!_leftView){
        _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(36), 32)];
        UIImageView *searchIcon = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), 8, 16, 16)];
        searchIcon.image = [UIImage imageNamed:@"searchGray16"];
        [_leftView addSubview:searchIcon];
    }
    return _leftView;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#429BFF" imageName:@""];
        _cancelBtn.frame = CGRectMake(Screen_W-viewPix(60), statusBarHeight+6, viewPix(60), 32);
        [_cancelBtn addTarget:self action:@selector(cancelSearchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStylePlain];
        [_tableView registerClass:[LGDeviceSendViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}


@end
